#include "Bullet.h"

Bullet::Bullet()
	: mActive(false)
	, mLife(1.7f)
	/*,kMaxBullets(100)
	,bulletIndex(0)*/// DISCUSS THIS NEXT TIME and CONST
{
	
};

Bullet::~Bullet()
{

};

void Bullet::Load()
{
	mSprite.Load("bullet2.png");

}; // initialize




void Bullet::Unload()
{
	mSprite.Unload();
}; // terminate

void Bullet::Update(float deltaTime,Map& mmap)
{
	if(mActive)
	{

		mPosition+= mVelocity*deltaTime;
		mLife-=deltaTime;
		if(mLife <= 0.0f)
		{
			Kill();
		}

		const int kWinWidth = 1500;
		const int kWinHeight = 1800;
		const int kThreshold = mSprite.GetWidth();
		/*if (mPosition.x < -kThreshold || mPosition.x > kWinWidth + kThreshold||
			mPosition.y < -kThreshold || mPosition.y > kWinHeight + kThreshold)
		{

			Kill();
		}*/

		
		if(mPosition.x <= 0.0f - (mSprite.GetWidth() * 0.5f))
		{
			mPosition.x = mmap.GetWidth() - 0.1f;
		}
		else if(mPosition.x >= mmap.GetWidth() + (mSprite.GetWidth() * 0.5f))
		{
			mPosition.x = mSprite.GetWidth()/2;
		}

		
		if(mPosition.y <= 0.0f - (mSprite.GetHeight() * 0.5f))
		{
			mPosition.y = mmap.GetHeight() - 0.5f;
		}
		else if(mPosition.y >= mmap.GetHeight() + (mSprite.GetHeight() * 0.5f))
		{
			mPosition.y = 0.0f;
		}

	}

};

void Bullet::Render(const SVector2 viewOffset)
{
	if (mActive)
	{
		int width = mSprite.GetWidth(); 
		int height = mSprite.GetHeight();
		SVector2 offset(width * 0.5f, height * 0.5f);
		SVector2 renderPosition = mPosition - offset-viewOffset; 
		mSprite.SetPosition(renderPosition);
		mSprite.Render();

	}
};




SVector2 Bullet:: GetPosition()
{

	return mPosition;

}

SVector2 Bullet:: GetVelocity()
{

	return mVelocity;

}


float Bullet:: GetLife()
{
	return mLife;
}


void Bullet::Fire(const SVector2& pos, const SVector2& vel)
{
	mPosition = pos;
	mVelocity = vel;
	mActive = true;
}

void Bullet::Kill()
{
	mActive = false;
	mLife = 10.0f;
}


bool Bullet::IsActive() const	
{
	return mActive;

}